﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DingHeng.TransportationManagement.Business;
using DingHeng.TransportationManagement.Entity.Constant;
using DingHeng.TransportationManagement.Entity;
using DingHeng.TransportationManagement.WebCommon;
using DingHeng.TransportationManagement.WebCommon.BaseUI;
using DingHeng.TransportationManagement.WebCommon.Extension;

namespace DingHeng.TransportationManagement.WebSite.Biz
{
    public partial class VehicleDispatchForm : BasePage
    {
        private DingHeng.TransportationManagement.Entity.VehicleDispatchForm VehicleDispatchFormInstance
        {
            get
            {
                if (ViewState["VehicleDispatchFormInstance"] == null)
                {
                    ViewState["VehicleDispatchFormInstance"] = new DingHeng.TransportationManagement.Entity.VehicleDispatchForm();
                }
                return ViewState["VehicleDispatchFormInstance"] as DingHeng.TransportationManagement.Entity.VehicleDispatchForm;
            }
            set
            {
                ViewState["VehicleDispatchFormInstance"] = value;
            }
        }
        private Guid? VehicleDispatchFormID
        {
            get
            {
                Guid id;
                if (string.IsNullOrEmpty(Request.QueryString.Get("VehicleDispatchFormID")) || Guid.TryParse(Request.QueryString.Get("VehicleDispatchFormID"), out id) == false)
                {
                    return null;
                }
                else
                {
                    return id;
                }
            }
        }
        private void initPage()
        {
            this.gridVehicel.DataSource = new List<VehicleInfo>();
            this.gridVehicel.DataBind();
            List<User> userList = new UserBiz().LoadActiveUsers();
            this.ddlPricipal.BindData<User>(userList);

            if (this.VehicleDispatchFormInstance.VehicleID.HasValue)
            {
                VehicleInfo info = new VehicleBiz().LoadByInfoID(this.VehicleDispatchFormInstance.VehicleID.Value);
                this.txtVehicle.Text = info.LicensePlateNo;
                this.txtVehicleOwner.Text = info.OwnerName;
                this.txtVehicleCode.Text = info.Code;
            }
            this.txtDriverPhoneNo.Text = this.VehicleDispatchFormInstance.DriverContactNo;
            this.txtDriver.Text = this.VehicleDispatchFormInstance.Driver;
            this.txtOwnerContactNo.Text = this.VehicleDispatchFormInstance.VehicleOwnerContactNo;
            this.txtProjectName.Text = this.VehicleDispatchFormInstance.ProjectName;
            if (this.VehicleDispatchFormInstance.PrincipalID.HasValue)
            {
                this.ddlPricipal.SelectedValue = this.VehicleDispatchFormInstance.PrincipalID.ToString();
            }
        }
        private void loadVehicleDispatchForm()
        {
            if (VehicleDispatchFormID.HasValue)
            {
                this.VehicleDispatchFormInstance = new VehicleDispatchFormBiz().LoadById(VehicleDispatchFormID.Value);
            }
            else
            {
                this.VehicleDispatchFormInstance = new Entity.VehicleDispatchForm();
                this.VehicleDispatchFormInstance.UpdateBy = this.VehicleDispatchFormInstance.CreateBy = WebContext.Current.CurrentUser.Name;
                this.VehicleDispatchFormInstance.UpdateDate = this.VehicleDispatchFormInstance.CreateDate = DateTime.Now;
                this.VehicleDispatchFormInstance.DispatchTime = DateTime.Now;
                this.VehicleDispatchFormInstance.ID = Guid.NewGuid();
                this.VehicleDispatchFormInstance.IsActive = true;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //if (VehicleDispatchFormID.HasValue)
                //{
                //    lblHomeTitle.Text = "更新派车单";
                //    this.btnSubmit.Text = "保存";
                //    this.VehicleDispatchFormInstance = new VehicleDispatchFormBiz().LoadById(VehicleDispatchFormID.Value);
                //}
                //else
                //{
                lblHomeTitle.Text = "新建派车单";
                this.btnSubmit.Text = "提交";
                this.VehicleDispatchFormInstance = new Entity.VehicleDispatchForm();
                this.VehicleDispatchFormInstance.UpdateBy = this.VehicleDispatchFormInstance.CreateBy = WebContext.Current.CurrentUser.Name;
                this.VehicleDispatchFormInstance.UpdateDate = this.VehicleDispatchFormInstance.CreateDate = DateTime.Now;
                this.VehicleDispatchFormInstance.DispatchTime = DateTime.Now;
                this.VehicleDispatchFormInstance.IsActive = true;
                //}
                initPage();

            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (this.VehicleDispatchFormInstance.VehicleID.HasValue)
            {
                if (this.ddlPricipal.SelectedIndex != 0)
                {
                    this.VehicleDispatchFormInstance.PrincipalID = Guid.Parse(this.ddlPricipal.SelectedValue);
                }
                this.VehicleDispatchFormInstance.DriverContactNo = this.txtDriverPhoneNo.Text;
                this.VehicleDispatchFormInstance.Driver = this.txtDriver.Text;
                this.VehicleDispatchFormInstance.VehicleOwnerContactNo = this.txtOwnerContactNo.Text;

                this.VehicleDispatchFormInstance.ProjectName = this.txtProjectName.Text;
                this.VehicleDispatchFormInstance.Status = Guid.Parse(StringConstant.StatusCheckIn);
                new VehicleDispatchFormBiz().Save(VehicleDispatchFormInstance);
                Response.Redirect(string.Format("VehicleDispatchFormView.aspx?VehicleDispatchFormID={0}", VehicleDispatchFormInstance.ID));
            }
            else
            {
                this.MessageBox("必须选择运输车辆！");
            }
        }

        protected void btnSearchVehicel_Click(object sender, EventArgs e)
        {
            //if (!string.IsNullOrEmpty(this.txtChooseVehicelNo.Text))
            //{
                List<VehicleInfo> vehicleInfoList = new VehicleBiz().LoadByActiveVehicleLicensePlateNo(this.txtChooseVehicelNo.Text);
                this.gridVehicel.DataSource = vehicleInfoList;
                this.gridVehicel.DataBind();
            //}
            this.WriteClientScript("chooseVehicel()");
        }


        protected void gridVehicel_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            this.VehicleDispatchFormInstance.VehicleID = Guid.Parse(this.gridVehicel.DataKeys[e.RowIndex]["VehicleID"].ToString());
            this.txtDriverPhoneNo.Text = this.gridVehicel.DataKeys[e.RowIndex]["DriverPhoneNo"] == null ? string.Empty : this.gridVehicel.DataKeys[e.RowIndex]["DriverPhoneNo"].ToString();
            this.txtOwnerContactNo.Text = this.gridVehicel.DataKeys[e.RowIndex]["OwnerContactNo"] == null ? string.Empty : this.gridVehicel.DataKeys[e.RowIndex]["OwnerContactNo"].ToString();

            txtVehicle.Text = this.gridVehicel.DataKeys[e.RowIndex]["LicensePlateNo"] == null ? string.Empty : this.gridVehicel.DataKeys[e.RowIndex]["LicensePlateNo"].ToString();
            this.txtVehicleOwner.Text = this.gridVehicel.Rows[e.RowIndex].Cells[2].Text;
            this.txtDriver.Text = this.gridVehicel.Rows[e.RowIndex].Cells[3].Text;
            this.txtVehicleCode.Text = this.gridVehicel.Rows[e.RowIndex].Cells[0].Text;

            this.txtChooseVehicelNo.Text = string.Empty;
            this.gridVehicel.DataSource = new List<VehicleInfo>();
            this.gridVehicel.DataBind();
        }
    }
}